package au.com.handsunited.web.controller.entity;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.service.EntityService;

/**
 *
 * @author arunda
 */
public class ProfileViewController extends AbstractController {


	private static Logger logger = LoggerFactory.getLogger(ProfileViewController.class);

	private EntityService entityService;
	private String successView;

	/**
	 *
	 */
	public ProfileViewController() {

	}

	/**
	 * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String entityId = request.getParameter("id");

		ModelAndView mav = new ModelAndView();

		if (StringUtils.isNotBlank(entityId)) {
			BaseEntity entity = entityService.getEntity(entityId);
			if (entity != null) {
				mav.setViewName(successView);
				mav.addObject("entity", entity);
			} else {
				logger.warn("Could not find entity for id[{}]", entityId);
			}
		}

		return mav;
	}

	/**
	 * @param entityService the entityService to set
	 */
	public void setEntityService(EntityService entityService) {
		this.entityService = entityService;
	}

	/**
	 * @param successView the successView to set
	 */
	public void setSuccessView(String successView) {
		this.successView = successView;
	}

}
